package com.cai.servlet.msg;

import com.cai.dao.MsgDao;
import com.cai.dao.impl.MsgDaoImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * @author RuoLi
 * 回复消息
 */
@WebServlet(name = "ServletReturnMessage", value = "/returnMessage")
public class ServletReturnMessage extends HttpServlet {
    String sendUser;
    String sendDate;
    String sendTime;
    String sendText;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收数据
        sendUser = request.getParameter("send_user");
        sendDate = request.getParameter("send_date");
        sendTime = request.getParameter("send_time");
        sendText = request.getParameter("send_text");
        HttpSession session = request.getSession();
        session.setAttribute("sendUser", sendUser);
        session.setAttribute("sendDate", sendDate);
        session.setAttribute("sendTime", sendTime);
        session.setAttribute("sendText", sendText);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        MsgDao msgDao = new MsgDaoImpl();
        //获取get数据
        String sendUser = this.sendUser;
        String sendDate = this.sendDate;
        String sendTime = this.sendTime;
        String returnText = request.getParameter("return_text");
        String returnUser = (String) session.getAttribute("user");
        int count = msgDao.returnMsg(returnText, sendDate, sendTime, sendUser, returnUser);
        if (count != 0) {
            //回复成功
            response.getWriter().println(1);
        } else {
            response.getWriter().println(0);
        }
    }
}
